package com.utility.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.utility.project.common.utils.ProjThreadPool;
import com.utility.project.entity.YsExperienceRecord;
import com.utility.project.mapper.YsExperienceRecordMapper;
import com.utility.project.param.ExperienceParam;
import com.utility.project.service.IYsCustomerService;
import com.utility.project.service.IYsExperienceRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author yangzhiguo
 * @date 2024-06-18
 * @description 
 */
@Service
public class YsExperienceRecordServiceImpl extends ServiceImpl<YsExperienceRecordMapper, YsExperienceRecord> implements IYsExperienceRecordService {

    @Resource
    IYsCustomerService iYsCustomerService;

    @Override
    public Boolean saveExperience(ExperienceParam param) {
        String userId = param.getUserId();
        Integer useNums = param.getUseNums();
        YsExperienceRecord ysExperienceRecord = new YsExperienceRecord();
        ysExperienceRecord.setUserId(userId);
        ysExperienceRecord.setDetail(param.getDetail());
        ysExperienceRecord.setUseTimes(useNums);
        ysExperienceRecord.setUseTime(new Date());
        ysExperienceRecord.setCreateBy("admin");
        ysExperienceRecord.setCreateTime(new Date());
        ysExperienceRecord.setPaperId(param.getPaperId());
        ProjThreadPool.getInstance().executor(()->{
            iYsCustomerService.reduceUseTimes(userId, useNums);
        });
        return this.saveOrUpdate(ysExperienceRecord);
    }


    @Override
    public List<YsExperienceRecord> getByUserName(String username) {
        QueryWrapper<YsExperienceRecord> query = new QueryWrapper<>();
        query.eq("user_id", username);
        return baseMapper.selectList(query);
    }
}
